package com.skivingcloud.admin.sys.controller;


import java.util.HashMap;
import java.util.Map;

import com.skivingcloud.admin.annotation.ModifyAnnotaionFactory;
import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.sys.entity.SysConfig;
import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.admin.sys.service.ISysConfigService;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.exception.BusinessException;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统参数配置信息表 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/sys/config")
public class SysConfigController {
	
	private static Logger log = LoggerFactory.getLogger(SysConfigController.class);

	@Autowired
	private ISysConfigService sysConfigService;

	/**
	 * 分页查询系统参数
	 * 
	 * @param params
	 * @return
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages = sysConfigService.queryPage(params);
		return R.ok().put("page", pages);
	}

	/**
	 * 新增系统参数
	 * 
	 * @param config
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "save", description = "新增系统参数", functionNo = "101052")
	@PostMapping("/save")
	public R save(@RequestBody SysConfig config) {
		try {
			User user = UserUtil.getUser();
			sysConfigService.save(config, user);
			// 给日志注解传入业务id
			ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "save", "bussid",
					config.toString(), config.getClass());
			return R.ok();
		} catch (BusinessException e) {
			return R.error(e.getMessage());
		} catch (Exception e) {
			log.error("保存系统参数失败", e);
			return R.error("保存系统参数失败");
		}
	}

	/**
	 * 修改系统参数
	 * 
	 * @param config
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "save", description = "修改系统参数", functionNo = "101053")
	@PostMapping("/update")
	public R update(@RequestBody SysConfig config) {
		try {
			User user = UserUtil.getUser();
			sysConfigService.update(config, user);
			// 给日志注解传入业务id
			ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "update", "bussid",
					config.getId(), config.getClass());
			return R.ok();
		} catch (Exception e) {
			log.error("保存系统参数失败", e);
			return R.error("保存系统参数失败");
		}
	}

	/**
	 * 删除配置
	 */
	@UserOperateLog(module = "系统功能", methods = "delete", description = "删除系统参数", functionNo = "101054")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			User user = UserUtil.getUser();
			sysConfigService.deleteBatch(ids);
			// 给日志注解传入业务id
			ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "update", "bussid",
					ids.toString(), ids.getClass());
			return R.ok();
		} catch (Exception e) {
			log.error("删除系统参数失败", e);
			return R.error("删除系统参数失败");
		}
	}

	/**
	 * 根据id查询系统参数
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) {
		SysConfig config = sysConfigService.selectById(id);
		return R.ok().put("config", config);
	}

	/**
	 * 根据参数key获取参数值，给外部调用，没有找到返回null
	 * 
	 * @param paramKey
	 * @return
	 */
	@GetMapping("/getParamVlueByKey/{paramKey}")
	public String getParamVlueByKey(@PathVariable("paramKey") String paramKey) {
		SysConfig config = sysConfigService.getParamVlueByKey(paramKey);
		return config == null ? null : config.getParamValue();
	}

	/**
	 * 检查参数编码是否存在
	 * 
	 * @param paramKey
	 * @return
	 */
	@GetMapping("/checkHasParamkey/{paramKey}")
	public R checkHasParamkey(@PathVariable("paramKey") String paramKey) {
		boolean ck = sysConfigService.checkHasParamkey(paramKey);
		if (ck) {
			return R.error("参数编码已被占用");
		}
		return R.ok();
	}
}
